/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mypomodoro;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author lifsong
 */
@SuppressWarnings("rawtypes")
public class Model <L extends ModelListener> {

    private final List<L> listeners;

    public Model() {
        this.listeners = new ArrayList<L>();
    }
    
    public void addModelListener(final L listener){
        if(! this.listeners.contains(listener) ){
            this.listeners.add(listener);
            notifyModelListner(listener);
        }
    }
    
    public boolean isModelListenerExisting(final L listener){
        return this.listeners.contains(listener);
    }
    
    public Integer getSpecificModeListenerCount(final L listner){
        Integer count = 0;
        
        for(final L existingListener : this.listeners){
            if(existingListener == listner){
                count++;
            }
        }
        return count;
    }
    
    public Integer getModeListenrsCount(){
        return this.listeners.size();
    }
    
    public void removeModelListener(final L listener){
        this.listeners.remove(listener);
    }

    @SuppressWarnings("unchecked")
	protected void notifyModelListner(L listener) {
        listener.modelChanged(this);
    }
    
    protected void notifyModelListeners() {
        for(final L listener : this.listeners){
        	notifyModelListner(listener);
        }
    }   
    
    protected class Property <T>{
        private T value;
        
        public Property(final T initValue){
            this.value = initValue;
        }
        
        public void setValue(final T value){
            if(this.value != value){
                this.value = value;
                notifyModelListeners();
            }
        }
        
        public T getValue(){
            return this.value;
        }
    }
}
